﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using Microsoft.Windows.Controls;
using System.Windows;

namespace SpeakupUI
{
    class TimeSheetPanel: ContentControl
    {
        private TimeSheet TimeSheet;
        private ComboBox StartTimeComboBox;
        private ComboBox EndTimeComboBox;
        

        public TimeSheetPanel(TimeSheet timeSheet)
        {
            TimeSheet = timeSheet;

            DateTime Begin = timeSheet.DateBegin;
            int days = (int)(timeSheet.DateEnd.Date - timeSheet.DateBegin.Date).TotalDays;


            Grid grid = new Grid();
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            grid.RowDefinitions.Add(new RowDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());
            grid.ColumnDefinitions.Add(new ColumnDefinition());


            {
                Label startLabel = new Label();
                startLabel.Margin = new Thickness(1);
                startLabel.Content = "Start date: ";
                startLabel.HorizontalAlignment = HorizontalAlignment.Right;

                grid.Children.Add(startLabel);
                Grid.SetRow(startLabel, 0);
                Grid.SetColumn(startLabel, 0);
            }

            {
                Label daysLabel = new Label();
                daysLabel.Margin = new Thickness(1);
                daysLabel.Content = "Days to show: ";
                daysLabel.HorizontalAlignment = HorizontalAlignment.Right;

                grid.Children.Add(daysLabel);
                Grid.SetRow(daysLabel, 1);
                Grid.SetColumn(daysLabel, 0);
            }


            {
                Label daysLabel = new Label();
                daysLabel.Margin = new Thickness(1);
                daysLabel.Content = "Time start: ";
                daysLabel.HorizontalAlignment = HorizontalAlignment.Right;

                grid.Children.Add(daysLabel);
                Grid.SetRow(daysLabel, 2);
                Grid.SetColumn(daysLabel, 0);
            }


            {
                Label daysLabel = new Label();
                daysLabel.Margin = new Thickness(1);
                daysLabel.Content = "Time  end: ";
                daysLabel.HorizontalAlignment = HorizontalAlignment.Right;

                grid.Children.Add(daysLabel);
                Grid.SetRow(daysLabel, 3);
                Grid.SetColumn(daysLabel, 0);
            }

            DatePicker datePicker;
            {
                datePicker = new DatePicker();
                datePicker.DisplayDateStart = DateTime.Now;
                datePicker.SelectedDate = Begin;
                datePicker.IsTodayHighlighted = true;
                datePicker.SelectedDateChanged += DateChanged;

                grid.Children.Add(datePicker);
                datePicker.Margin = new Thickness(1);
                Grid.SetRow(datePicker, 0);
                Grid.SetColumn(datePicker, 1);
            }

            ComboBox daysComboBox;
            {
                daysComboBox = new ComboBox();
                daysComboBox.HorizontalContentAlignment = HorizontalAlignment.Right;
                for (int i = 0; i < 20; i++)
                {
                    daysComboBox.Items.Add(i + 1);
                }

                daysComboBox.SelectedIndex = days;
                daysComboBox.SelectionChanged += LengthChanged;

                grid.Children.Add(daysComboBox);
                daysComboBox.Margin = new Thickness(1);

                Grid.SetRow(daysComboBox, 1);
                Grid.SetColumn(daysComboBox, 1);
            }
            
            {
                StartTimeComboBox = new ComboBox();
                StartTimeComboBox.HorizontalContentAlignment = HorizontalAlignment.Right;
                                
                grid.Children.Add(StartTimeComboBox);
                StartTimeComboBox.Margin = new Thickness(1);

                Grid.SetRow(StartTimeComboBox, 2);
                Grid.SetColumn(StartTimeComboBox, 1);
            }


            {
                EndTimeComboBox = new ComboBox();
                EndTimeComboBox.HorizontalContentAlignment = HorizontalAlignment.Right;
                

                grid.Children.Add(EndTimeComboBox);
                EndTimeComboBox.Margin = new Thickness(1);

                Grid.SetRow(EndTimeComboBox, 3);
                Grid.SetColumn(EndTimeComboBox, 1);                               
            }

            UpdateTimeComboBoxes();

            {
                Button resetBtn = new Button();
                resetBtn.Content = "Reset to optimal";
                resetBtn.Click += delegate
                {
                    DateTime now = DateTime.Now.Date;
                    int _days = 14;
                    TimeSheet.TimeBegin = new TimeSpan(8, 0, 0);
                    TimeSheet.TimeEnd = new TimeSpan(22, 0, 0);

                    daysComboBox.SelectedIndex = _days - 1;
                    datePicker.SelectedDate = now;

                    TimeSheet.UpdateDate(now, now + new TimeSpan(_days - 1, 0, 0, 0));                   

                    UpdateTimeComboBoxes();
                };
                resetBtn.Margin = new Thickness(1);

                grid.Children.Add(resetBtn);
                Grid.SetRow(resetBtn, 4);
                Grid.SetColumn(resetBtn, 0);
                Grid.SetColumnSpan(resetBtn, 2);
            }

            GroupBox box = new GroupBox();
            box.Header = "Time/Date settings";
            box.Content = grid;
            box.Padding = new Thickness(2);

            Content = box;
        }

        void UpdateTimeComboBoxes()
        {
            EndTimeComboBox.SelectionChanged -= EndTimeChanged;
            StartTimeComboBox.SelectionChanged -= StartTimeChanged;

            FillTimeComboBox(StartTimeComboBox, new TimeSpan(),
                                                    TimeSheet.TimeEnd - new TimeSpan(1, 0, 0));
            SelectTimeComboBox(StartTimeComboBox, (int)TimeSheet.TimeBegin.TotalHours);


            FillTimeComboBox(EndTimeComboBox, TimeSheet.TimeBegin + new TimeSpan(1, 0, 0),
                                                 new TimeSpan(24, 0, 0));
            SelectTimeComboBox(EndTimeComboBox, (int)TimeSheet.TimeEnd.TotalHours);

            EndTimeComboBox.SelectionChanged += EndTimeChanged;
            StartTimeComboBox.SelectionChanged += StartTimeChanged;

        }

        void FillTimeComboBox(ComboBox comboBox, TimeSpan begin, TimeSpan last)
        {
            comboBox.Items.Clear();
            String strFormat = "{0,2:00}:{1,2:00}";

            for (int i = (int)begin.TotalHours; i <= (int)last.TotalHours; i++)
            {
                TimeSpan span = new TimeSpan(i, 0, 0);
                string text = String.Format(strFormat, span.TotalHours, span.Minutes);

                ComboBoxItem item = new ComboBoxItem();
                item.Tag = span;
                item.Content = text;
                
                comboBox.Items.Add(item);
             }
        }

        void SelectTimeComboBox(ComboBox comboBox, int hour)
        {            
            foreach (ComboBoxItem item in comboBox.Items)
            {
                TimeSpan timeSpan = (TimeSpan)item.Tag;
                if (timeSpan.TotalHours == hour)
                {
                    comboBox.SelectedItem = item;
                    break;
                }
            }
        }

        void DateChanged(object sender, SelectionChangedEventArgs e)
        {
            DatePicker picker = (DatePicker)sender;

            DateTime selectedDate = picker.SelectedDate ?? DateTime.Now.Date;
            TimeSpan offset = TimeSheet.DateEnd.Date - TimeSheet.DateBegin.Date;            
            TimeSheet.UpdateDate(selectedDate, selectedDate + offset);
        }

        void LengthChanged(object sender, SelectionChangedEventArgs e)
        {
            ComboBox box = (ComboBox)sender;

            int days = box.SelectedIndex;

            TimeSheet.DateEnd = TimeSheet.DateBegin.Date + new TimeSpan(days, 0, 0, 0);
        }

        void StartTimeChanged(object sender, SelectionChangedEventArgs e)
        {
            if (StartTimeComboBox.SelectedIndex < 0) return;
                        
            ComboBoxItem item = (ComboBoxItem)StartTimeComboBox.Items[StartTimeComboBox.SelectedIndex];
            TimeSpan timeSpan = (TimeSpan)item.Tag;

            ComboBoxItem endItem = (ComboBoxItem)EndTimeComboBox.Items[0];
            TimeSpan endTimeSpan = (TimeSpan)endItem.Tag;
            if ((int)endTimeSpan.TotalHours == (int)timeSpan.TotalHours + 1)
            {
                return;
            }

            FillTimeComboBox(EndTimeComboBox, timeSpan + new TimeSpan(1, 0, 0), new TimeSpan(24, 0, 0));
            SelectTimeComboBox(EndTimeComboBox, (int)TimeSheet.TimeEnd.TotalHours);
            TimeSheet.TimeBegin = timeSpan;
        }

        void EndTimeChanged(object sender, SelectionChangedEventArgs e)
        {
            if (EndTimeComboBox.SelectedIndex < 0) return;

            ComboBoxItem item = (ComboBoxItem)EndTimeComboBox.Items[EndTimeComboBox.SelectedIndex];
            TimeSpan timeSpan = (TimeSpan)item.Tag;

            int lastIndex = StartTimeComboBox.Items.Count - 1;
            ComboBoxItem beginItem = (ComboBoxItem)StartTimeComboBox.Items[lastIndex];
            TimeSpan startTimeSpan = (TimeSpan)beginItem.Tag;
            if ((int)startTimeSpan.TotalHours == (int)timeSpan.TotalHours - 1)
            {
                return;
            }

            FillTimeComboBox(StartTimeComboBox, new TimeSpan(0, 0, 0), timeSpan - new TimeSpan(1, 0, 0));
            SelectTimeComboBox(StartTimeComboBox, (int)TimeSheet.TimeBegin.TotalHours);
            TimeSheet.TimeEnd = timeSpan;
        }
    }
}
